package com.practice.J2E.util;

import cn.hutool.http.HttpRequest;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.practice.J2E.util.dto.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 主数据--空运
 * @author kayn
 * @create 2023-12-11 下午 02:00
 */
@Component
public class AirDataUtil {


    public static void main(String[] args) {

        String code = "CKG";

        AirportDTO airportDTO = matchAirportByCode(code);

        System.out.println(airportDTO);
    }


    /**
     * 根据港口三字码查询机场
     * @param code 三字码
     * @return 机场信息
     */
    public static AirportDTO matchAirportByCode(String code){

        //在此处编码

        MainDTO mainDTO = new MainDTO();
        mainDTO.setCode(code);
        mainDTO.setToken("6BFBA86331428D7162617BD75163DDC0F896471D3E7AF1D1BE0785D26777F52B387C0E27D8F90D9028DC4452C5A5266B8E104560CF6EE24CE7AF7131559113A87709C2242FCC9731852543604F66A53AF17575A518FADA80C91344D94C2337C9D0C9809232A52006FF357F16A18EC255E7F089D1323C359CEA544C95DAED96CD4751EC4ADA10F274492270733FD55B82C75F5FD15C8AC4A5635CA18C0D5DEFC9");

        HeadDTO headDTO = new HeadDTO();
        headDTO.setAction("query");

        MainDataParam mainDataParam = new MainDataParam();
        mainDataParam.setHead(headDTO);
        mainDataParam.setMain(mainDTO);

        String jsonString = JSON.toJSONString(mainDataParam);

        String responseJson = HttpRequest.post("http://mdd.bondex.com.cn:9085/maindata/api/air/airport")
                .body(jsonString)
                .timeout(20000)
                .execute()
                .body();

        MainDataResult<List<AirportDTO>> result = JSONObject.parseObject(responseJson,new TypeReference<MainDataResult<List<AirportDTO>>>() {});


        List<AirportDTO> array = result.getData();

        if (array != null && array.size() > 0){
            return array.get(0);
        }

        return null;
    }

}
